package main

import (
	"fmt"
	"math"
	"os"
	"path/filepath"
	"strconv"
)

type Books struct {
	bookId  int
	title   string
	author  string
	price   float32
	subject string
}

func main() {
	book1 := Books{1, "Go", "Gopher", 100.0, "Programming"}
	fmt.Println(book1)
	fmt.Println(book1.title)
	countryCapitalMap()
	fmt.Println("遍历目录")
	walkDir(".", "")
	toInt, _ := ConvertStrToInt("20", 10)
	println(toInt)
	c := circle{10}
	var s Shape = c
	fmt.Println("面积是:", s.Area())
	fmt.Println("周长是:", s.perimeter())
}

func countryCapitalMap() {
	capitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo"}
	fmt.Println("原始地图")
	for country, capital := range capitalMap {
		fmt.Println(country, "首都是", capital)
	}
	delete(capitalMap, "France")
	fmt.Println("法国被删除")
	for country, capital := range capitalMap {
		fmt.Println(country, "首都是", capital)
	}
}

func walkDir(dir string, indent string) {
	entries, err := os.ReadDir(dir)
	if err != nil {
		return
	}
	for _, entry := range entries {
		fmt.Println(indent, entry.Name())
		if entry.IsDir() {
			walkDir(filepath.Join(dir, entry.Name()), indent+" ")
		}
	}
}

func satoi() {
	str := "123a"
	num, err := strconv.Atoi(str)
	if err != nil {
		fmt.Println("转化错误")
	} else {
		fmt.Printf("字符创 '%s' 转化成整数为:%d \n", str, num)
	}
}

func ConvertStrToInt(str string, base int) (int64, error) {
	return strconv.ParseInt(str, base, 64)
}

// Shape 定义一个接口
type Shape interface {
	Area() float64
	perimeter() float64
}

// 定义一个结构体
type circle struct {
	radius float64
}

// Area Circle 实现Shape的Area方法
func (c circle) Area() float64 {
	return math.Pi * c.radius * c.radius
}

// perimeter Circle 实现Shape的perimeter方法
func (c circle) perimeter() float64 {
	return 2 * math.Pi * c.radius
}
